package jyan.sango.crazy.core.xml;

import java.io.File;
import java.util.List;

import jyan.sango.crazy.core.card.CardCategory;
import jyan.sango.crazy.core.debug.DEBUG;
import jyan.sango.crazy.core.debug.SangoException;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReadFile {
	private final static Element getAllElements(final File f) {
		Document doc = null;
		Element root = null;
		try {
			if (!f.exists()) {
				System.out.println("  Error : Config file doesn't exist!");
				System.exit(1);
			}
			SAXReader reader = new SAXReader();
			doc = reader.read(f);
			root = doc.getRootElement();
		} catch (Exception ex) {
			new SangoException();
		}
		return root;

	}

	public static List<?> getElementList(File f, XMLType type)
			throws SangoException {
		Element root = getAllElements(f);
		String strname = null;
		String strtext = null;
		try {
			if ("card".equals(type)) {
				strname = root.attribute("category").getName();
				strtext = root.attribute("category").getText();
				setCardCategory(strname, strtext);
			}
			DEBUG.Print("What?category!:" + strtext);
		} catch (NullPointerException ex) {
			throw new SangoException("No Category Find");
		}
		return root.elements();
	}

	public static List<?> getElementList(File f) throws SangoException {
		return getElementList(f, null);
	}

	private static void setCardCategory(final String strname,
			final String strtext) {
		Integer ccate = null;
		if ("category".equals(strname)) {
			ccate = CardCategory.getCardCategory(strtext);
			ReadCard.setCardcategory(ccate);
		}
	}
}
